﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MYTech.Data
{
    /// <summary>
    /// 自定义生成数据库连接串
    /// </summary>
    public class DBConnection
    {
        private DBType _DataBaseType { get; set; }
        private string _IP { get; set; }
        private string _DataBaseName { get; set; }
        private string _UserName { get; set; }
        private string _Password { get; set; }
        private int _Port { get; set; }
        private string _DataSource
        {
            get
            {
                string str = null;
                switch (_DataBaseType)
                {
                    case DBType.Oracle:
                        str = string.Format("Data Source={0}:{1}/{2};", _IP, _Port, _DataBaseName);
                        break;
                    case DBType.SQLServer:
                        str = string.Format("Server={0},{1};Initial Catalog={2};", _IP, _Port, _DataBaseName);
                        break;
                    case DBType.MySql:
                        str = string.Format("Database={2};Data Source={0};pooling=false;CharSet=utf8;port={1};", _IP, _Port, _DataBaseName);
                        break;
                    case DBType.Sybase:
                        str = string.Format("Data Source={0};Port={1};Database={2};", _IP, _Port, _DataBaseName); break;
                }
                return str;
            }
        }

        private string _DataBaseFileName { get; set; }
        public DBConnection(DBType dataBaseType, string dataBaseFileName, string userName, string password)
        {
            if (dataBaseType == DBType.Access || dataBaseType == DBType.SQLite)
            {
                this._DataBaseType = dataBaseType;
                this._DataBaseFileName = dataBaseFileName;
                this._UserName = userName;
                this._Password = password;
            }
        }

        public DBConnection(DBType dataBaseType, string ip, string dataBaseName, int port, string userName, string password)
        {
            this._DataBaseType = dataBaseType;
            this._DataBaseName = dataBaseName;
            this._IP = ip;
            this._Port = port;
            this._UserName = userName;
            this._Password = password;
        }

        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <returns></returns>
        public string DBConnectionString
        {
            get
            {
                string connStr = null;
                switch (_DataBaseType)
                {
                    case DBType.Access:
                        connStr = string.Format("Data Source={0}", _DataBaseFileName);
                        break;
                    case DBType.DB2: break;
                    case DBType.MySql:
                        connStr = string.Format("{0}User Id={1};Password={2}", _DataSource, _UserName, _Password);
                        break;
                    case DBType.Oracle:
                        connStr = string.Format("{0}User Id={1};Password={2}", _DataSource, _UserName, _Password);
                        break;
                    case DBType.SQLite:
                        if (string.IsNullOrEmpty(_UserName) || string.IsNullOrEmpty(_Password))
                            connStr = string.Format("Data Source={0}", _DataBaseFileName);
                        else
                            connStr = string.Format("Data Source={0};Password={1}", _DataBaseFileName, _Password);
                        break;
                    case DBType.SQLServer:
                        connStr = string.Format("{0}User ID={1};Password={2}", _DataSource, _UserName, _Password);
                        break;
                    case DBType.Sybase:
                        connStr = string.Format("{0}UID={1};PWD={2}", _DataSource, _UserName, _Password);
                        break;
                }
                return connStr;
            }
        }
    }

    public enum DBType
    {
        Oracle,
        SQLServer,
        MySql,
        SQLite,
        Access,
        DB2,
        Sybase
    }
}
